<?php
/**
 * Created by PhpStorm.
 * User: Duyonggao
 * Date: 2017/8/2 0002
 * Time: 上午 9:32
 */

namespace utils;


class HelperSearch
{
    /**
     * 申请的appkey
     */
    const APP_KEY = '9F3mUpU5';

    /**
     * 注册的uid
     */
    const UID = '62475';

    const VERSION = '1.0';

    private $link = "http://openapi.qingtaoke.com/qingsoulist?";
    private $search_link = "http://openapi.qingtaoke.com/search?";
    private $gx_link = "http://qtkexe.qingtaoke.com/index.php?";
    private $bank_link = "http://openapi.qingtaoke.com/baokuan?";
    private $work_link = "http://openapi.qingtaoke.com/hot?";
    private $prox = "http://ceshi.weiao88.com/index.php/index/url";

    private $get_data = [
        'sort' => 1,
        'page' => 1,
        'app_key' => self::APP_KEY,
        'v' => self::VERSION,
        'cat' => 0,
        'min_price' => 1,
        'max_price' => 9999,
        'new'=>0,
        'is_ju'=> 0,
        'is_tqg' =>0,
    ];

    private $good_data = [
        'sort' => 1,
        's_type' => 1,
        'key_word' => '',
        'app_key' => self::APP_KEY,
        'page' => 1,
        'v' => self::VERSION,
        'cat' => 0,
        'min_price' => 1,
        'max_price' => 9999,
        'new'=>0,
        'is_ju'=> 0,
        'is_tqg' =>0,
    ];

    private $gx_data = [
        'r' => 'cmsExeApi/transferUrl',
        'uid' => self::UID,
        'activityId' => '',
        'itemId' => '',
    ];

    private $bank_data = [
        'app_key' => self::APP_KEY,
        'v' => self::VERSION,
    ];

    private $work_data = [
        'app_key' => self::APP_KEY,
        'v' => self::VERSION,
        't' => 1,
    ];

    private static $not_in = ['app_key','v','r'];

    public static $couponMap = [
        0 => '全部',
        '10'=>'女装',
        '11'=>'内衣',
        '12'=>'男装',
        '2'=>'母婴',
        '3'=>'美妆',
        '4'=>'居家',
        '5'=>'鞋包配饰',
        '6'=>'美食',
        '7'=>'文体',
        '8'=>'家电数码',
    ];

    /**
     * @author       :   DuYongGao
     * @name         :   getData
     * @description  :取列表数据
     * @param array $data
     * @return mixed
     */
    public function getData($data = []){
        $tmp_data = $this->get_data;
        if(!empty($data)){
            foreach($data as $k => $vo){
                if(!in_array($k,static::$not_in) && array_key_exists($k,$tmp_data) && !empty($vo)){
                    $tmp_data[$k] = $vo;
                }
            }
        }
        $key = md5('data'.json_encode($tmp_data).'4');
        $json = \Yii::$app->cache->get($key);
        if($json!== false){
            return $json;
        }
        $link = $this->link.http_build_query($tmp_data);
        $curl = new Curl();
        $curl->setOpt(CURLOPT_CONNECTTIMEOUT , 5);
        $content = $curl->post($this->prox,['url'=>$link]);
        $json = json_decode(trim($content->response,"1"),true);
        \Yii::$app->cache->set($key,$json,60);
        return $json;
    }

    /**
     * @author       :   DuYongGao
     * @name         :   getGood
     * @description  :查询商品列表
     * @param $data
     * @return mixed
     */
    public function getGood($data){
        $tmp_data = $this->good_data;
        if(!empty($data)){
            foreach($data as $k => $vo){
                if(!in_array($k,static::$not_in) && array_key_exists($k,$tmp_data) && !empty($vo)){
                    $tmp_data[$k] = $vo;
                }
            }
        }
        $key = md5('good'.json_encode($tmp_data));
        $json = \Yii::$app->cache->get($key);
        if($json!== false){
            return $json;
        }
        $link = $this->search_link.http_build_query($tmp_data);
        $curl = new Curl();
        $curl->setUserAgent('Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HTC D820u Build/KTU84P) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 SogouMSE,SogouMobileBrowser/3.5.1');
        $curl->setOpt(CURLOPT_CONNECTTIMEOUT , 5);
        $content = $curl->post($this->prox,['url'=>$link]);
        $json = json_decode(trim($content->response,"1"),true);
        \Yii::$app->cache->set($key,$json,60);
        return $json;
    }

    /**
     * @author       :   DuYongGao
     * @name         :   getGood
     * @description  :查询商品列表
     * @param $data
     * @return mixed
     */
    public function getDetail($data){
        if(empty($data)){
           return false;
        }
        $key = md5(json_encode($data));
        $json = \Yii::$app->cache->get($key);
        if($json!== false){
            if(!empty($json['data']['url'])){
                $json['data']['url'] = Comm::J($json['data']['url']);
            }
            return $json;
        }
        $tmp_data = $this->gx_data;
        if(!empty($data)){
            foreach($data as $k => $vo){
                if(!in_array($k,static::$not_in) && array_key_exists($k,$tmp_data)){
                    $tmp_data[$k] = $vo;
                }
            }
        }
        $link = $this->gx_link.http_build_query($tmp_data);
        $curl = new Curl();
        $curl->setUserAgent('Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HTC D820u Build/KTU84P) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 SogouMSE,SogouMobileBrowser/3.5.1');
        $curl->setOpt(CURLOPT_CONNECTTIMEOUT , 5);
        $content = $curl->post($this->prox,['url'=>$link]);
        $json = json_decode(trim($content->response,"1"),true);
        if(!empty($json)){
            if($data['activityId'] == md5(1)){
                $url = $json['data']['url'];
                $json['data']['url'] = static::getLongUrl($url);
                $json['data']['url'] = preg_replace('/activityId='.md5(1).'/',"",$json['data']['url'] );
            }
            \Yii::$app->cache->set($key,$json,86400);
            if(!empty($json['data']['url'])){
                $json['data']['url'] = Comm::J($json['data']['url']);
            }
        }
        return $json;
    }

    /**
     * @author       :   DuYongGao
     * @name         :   getKeyList
     * @description  :读取热词列表
     * @return mixed
     */
    public function getKeyList(){
        $tmp_data = $this->work_data;
        $key = md5(json_encode($tmp_data).'1');
        $json = \Yii::$app->cache->get($key);
        if($json!== false){
            return $json;
        }

        $link = $this->work_link.http_build_query($tmp_data);
        $curl = new Curl();
        $curl->setUserAgent('Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HTC D820u Build/KTU84P) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 SogouMSE,SogouMobileBrowser/3.5.1');
        $curl->setOpt(CURLOPT_CONNECTTIMEOUT , 5);
        $content = $curl->post($this->prox,['url'=>$link]);
        $json = json_decode(trim($content->response,"1"),true);
        if(!empty($json)){
            \Yii::$app->cache->set($key,$json,86400);
        }
        return $json;
    }

    /**
     * @author       :   DuYongGao
     * @name         :   getWord
     * @description  :
     * @return array
     */
    public function getWord(){
        $data = $this->getKeyList();
        if(isset($data['data'])){
            return $data['data'];
        }else{
            return [];
        }
    }

    /**
     * @author       :   DuYongGao
     * @name         :   getBank
     * @description  :查询热销榜单
     * @return mixed
     */
    public function getBank(){
        $tmp_data = $this->bank_data;
        $key = md5('bank'.json_encode($tmp_data));
        $json = \Yii::$app->cache->get($key);
        if($json!== false){
            return $json;
        }
        $link = $this->bank_link.http_build_query($tmp_data);
        $curl = new Curl();
        $curl->setUserAgent('Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HTC D820u Build/KTU84P) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 SogouMSE,SogouMobileBrowser/3.5.1');
        $curl->setOpt(CURLOPT_CONNECTTIMEOUT , 5);
        $content = $curl->post($this->prox,['url'=>$link]);
        $json = json_decode(trim($content->response,"1"),true);
        \Yii::$app->cache->set($key,$json,60);
        return $json;
    }

    public static function getLongUrl($url) {
        $headers = get_headers($url, TRUE);
        if(isset($headers['Location']) && is_string($headers['Location'])){
            return $headers['Location'];
        }elseif(isset($headers['Location']) && is_array($headers['Location'])){
            return $headers['Location'][0];
        }
        return false;
    }
}